package com.clms.marketings.service.Impl;

//import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.clms.marketings.mapper.SmsAdvertisePositionMapper;
import com.clms.marketings.pojo.entity.SmsAdvertisePosition;
import com.clms.marketings.pojo.vo.AdvertisePositionVo;
import com.clms.marketings.pojo.vo.AdvertiseTypeVo;
import com.clms.marketings.service.SmsAdvertisePositionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: clms-third
 * @description: 广告位置的业务逻辑层
 * @author: 赵露露
 * @create: 2021-07-23 19:25
 **/
@Service
public class SmsAdvertisePositionServiceImpl implements SmsAdvertisePositionService {
    @Autowired
    SmsAdvertisePositionMapper smsAdvertisePositionMapper;
    @Override
    public R selectAll() {
        List<SmsAdvertisePosition> selectAll=smsAdvertisePositionMapper.selectList(null);
        return R.ok().put("allPosition",selectAll);
    }

    @Override
    public R insertPosition(SmsAdvertisePosition smsAdvertisePosition) {
        int insertPosition= smsAdvertisePositionMapper.insert(smsAdvertisePosition);
        return R.ok();
    }

    @Override
    public R updatePosition(SmsAdvertisePosition smsAdvertisePosition) {
        int updatePosition= smsAdvertisePositionMapper.updateById(smsAdvertisePosition);
        return R.ok();
    }

    @Override
    public R deletePosition(Integer id) {
        int deletePosition=smsAdvertisePositionMapper.deleteById(id);
        return R.ok();
    }

    @Override
    public R selectById(Integer id) {
        SmsAdvertisePosition smsAdvertisePosition=smsAdvertisePositionMapper.selectById(id);
        return R.ok().put("previews",smsAdvertisePosition);
    }

    @Override
    public R selectPage(Integer currentPage, Integer pageSize, AdvertisePositionVo advertisePositionVo) {
        Page<SmsAdvertisePosition> page=new Page<>(currentPage,pageSize);
        QueryWrapper<SmsAdvertisePosition> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(advertisePositionVo.getSmsName())){
            queryWrapper.like("sms_name",advertisePositionVo.getSmsName());
        }
        Page<SmsAdvertisePosition> page1= smsAdvertisePositionMapper.selectPage(page,queryWrapper);
        return R.ok().put("total",page1.getTotal()).put("records",page1.getRecords());
    }

    @Override
    public R nameUnique(AdvertiseTypeVo advertiseTypeVo) {
        QueryWrapper wrapper=new QueryWrapper();
        if (StringUtils.isNotEmpty(advertiseTypeVo.getType())){
            wrapper.eq("type",advertiseTypeVo.getType());
        }
        if (StringUtils.isNotEmpty(advertiseTypeVo.getId())){
            wrapper.ne("id",advertiseTypeVo.getId());
        }
        if (StringUtils.isNotEmpty(advertiseTypeVo.getType())){
            SmsAdvertisePosition smsAdvertisePosition = smsAdvertisePositionMapper.selectOne(wrapper);
            if (smsAdvertisePosition!=null){
                return R.error().message("类型存在");
            }else {
                return R.ok().message("可以添加");
            }
        }else {
            return R.error().message("不能为空");
        }


    }
}
